package com.dr.dac.knowledgebase.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.dr.dac.knowledgebase.ui.bean.KContent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weifeiyang on 2016/12/26.
 */

public class KContentDataBase {
    private final DatabaseHelper dbHelper;
    public KContentDataBase(Context context) {
        super();
        dbHelper =DatabaseHelper.getInstance(context);

        //dropTable();
        //createTable();
    }

    /**
     * 增
     *
     * @param data
     */
    public void insert(KContent data) {
        String sql = "replace into " + DatabaseHelper.TABLE_NAME_K_Content;

        sql += "(ContentID,FPointID,ClassMID,ContentName,Author,Content,PublishTime,Field,Source,KeyWord)"
                + "values(?,?,?,?,?,?,?,?,?,?)";

        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL(sql, new String[]{data.getContentID(),data.getFPointID(),
                data.getClassMID(),data.getContentName(),data.getAuthor(),data.getContent(),data.getPublishTime(),
                data.getField(),data.getSource(),data.getKeyWord()});
        sqlite.close();
    }

    /**
     * 删
     *
     * @param ContentID
     */
    public void delete(String ContentID) {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        String sql = ("delete from " + DatabaseHelper.TABLE_NAME_K_Content + " where ContentID=?");
        sqlite.execSQL(sql, new String[]{ContentID});
        sqlite.close();
    }

    /**
     * 改
     *
     * @param data
     */
    public void updateAvatar(KContent data) {
//        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
//        String sql = ("update " + DatabaseHelper.TABLE_NAME_K_Content + " set avatar=? where userId=?");
//        sqlite.execSQL(sql, new String[]{data.getAvatar(), data.getUserId()});
//        sqlite.close();
    }


    public List<KContent> query() {
        return query(" ");
    }

    /**
     * 查
     *
     * @param where
     * @return
     */
    public List<KContent> query(String where) {
        SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
        ArrayList<KContent> data = null;
        data = new ArrayList<KContent>();
        Cursor cursor = sqlite.rawQuery("select * from "
                + DatabaseHelper.TABLE_NAME_K_Content + where, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            KContent info = new KContent();
            info.setContentID(cursor.getString(0));
            info.setFPointID(cursor.getString(1));
            info.setClassMID(cursor.getString(2));
            info.setContentName(cursor.getString(3));
            info.setAuthor(cursor.getString(4));
            info.setContent(cursor.getString(5));
            info.setPublishTime(cursor.getString(6));
            info.setField(cursor.getString(7));
            info.setSource(cursor.getString(8));
            info.setKeyWord(cursor.getString(9));
            data.add(info);
        }

        if (!cursor.isClosed()) {
            cursor.close();
        }
        sqlite.close();

        return data;
    }

    /**
     * 重置
     *
     * @param data
     */
    public void reset(KContent data) {
        if (data != null) {
            SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
            // 删除全部
            sqlite.execSQL("delete from " + DatabaseHelper.TABLE_NAME_K_Content);
            // 重新添加
            insert(data);

            sqlite.close();
        }
    }

    /**
     * 保存数据到本地 (若已存在则直接覆盖)
     *
     * @param data
     */
    public void save(KContent data) {
        List<KContent> datas = query(" where ContentID = " + data.getContentID());
        if (datas != null && !datas.isEmpty()) {
            delete(data.getContentID());
            insert(data);
        } else {
            insert(data);
        }
    }


    public void destroy() {
        dbHelper.close();
    }

    /**
     * 删除表
     */
    public void dropTable() {

        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL("DROP TABLE IF EXISTS " + DatabaseHelper.TABLE_NAME_K_Content);
        sqlite.close();
    }

    /**
     * 新建表
     */
    public void createTable() {
        SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
        sqlite.execSQL(DatabaseHelper.CREATE_TABLE_K_Class);
        sqlite.close();
    }
}
